package com.ddf.publics.service.impl;

import com.ddf.publics.entity.UserOrder;
import com.ddf.publics.dao.UserOrderDao;
import com.ddf.publics.service.UserOrderService;
import com.ddf.publics.tool.Constant;
import com.ddf.publics.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * (UserOrder)表服务实现类
 *
 * @author makejava
 * @since 2020-06-25 10:07:11
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {
    @Resource
    private UserOrderDao userOrderDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void editOrder(UserOrder userOrder,Integer day){
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_MONTH,day);
        Date date = cal.getTime();

        long begin;
        long end;
        begin= userOrder.getDeliveryTime().getTime();
        end = date.getTime();
        int time = (int)((end - begin) / 1000);
        try {
            redisUtil.set("order:"+userOrder.getOrderId(),"猪心",time);
            update(userOrder);
        }catch (Exception e){
            return;
        }
    }

    public void redisOrder(String keyid){
        UserOrder userOrder = new UserOrder();
        userOrder.setOrderId(Integer.valueOf(keyid));
        userOrder.setOrderState(Constant.TO_BE_RECEIVED);  //状态改为待收货
        update(userOrder);
    }

    @Override
    public int orderCount(Integer id) {
        return userOrderDao.orderCount(id);
    }

    public int MorderCount() {
        return userOrderDao.MorderCount();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public UserOrder queryById(Integer orderId) {
        return this.userOrderDao.queryById(orderId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<UserOrder> queryAllByLimit(int offset, int limit,int userid) {
        return this.userOrderDao.queryAllByLimit(offset, limit,userid);
    }


    @Override
    public List<UserOrder> MqueryAllByLimit(int offset, int limit) {
        return this.userOrderDao.MqueryAllByLimit(offset, limit);
    }
    /**
     * 新增数据
     *
     * @param userOrder 实例对象
     * @return 实例对象
     */
    @Override
    public UserOrder insert(UserOrder userOrder) {
        this.userOrderDao.insert(userOrder);
        return userOrder;
    }

    /**
     * 修改数据
     *
     * @param userOrder 实例对象
     * @return 实例对象
     */
    @Override
    public UserOrder update(UserOrder userOrder) {
        this.userOrderDao.update(userOrder);
        return this.queryById(userOrder.getOrderId());
    }

    /**
     * 通过主键删除数据
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer orderId) {
        return this.userOrderDao.deleteById(orderId) > 0;
    }
}